const SegmentTree = require("../template")

function testSegmentTreeLazyPropagation() {
  const arr = [1, 2, 3, 4, 5, 6, 7, 8]
  const st = new SegmentTree(arr)

  console.log(
    "1. 懒加载区间更新测试:",
    (() => {
      st.updateRangeValue(0, 3, 2) // 给前4个元素都加2
      return st.getRangeSum(0, 3) === 18 && // (1+2)+(2+2)+(3+2)+(4+2)=18
             st.getRangeSum(4, 7) === 26 // 5+6+7+8=26
    })()
  )

  console.log(
    "2. 多次懒加载更新测试:",
    (() => {
      st.updateRangeValue(2, 5, 3) // 给中间4个元素都加3
      return st.getRangeSum(2, 5) === 32 && // (3+2+3)+(4+2+3)+(5+3)+(6+3)=32
             st.getRangeSum(0, 1) === 7 // (1+2)+(2+2)=7
    })()
  )

  console.log(
    "3. 重叠区间懒加载测试:",
    (() => {
      st.updateRangeValue(1, 4, 1) // 给中间4个元素都加1
      return st.getRangeSum(1, 4) === 25 && // (2+2+1)+(3+2+3+1)+(4+2+3+1)+(5+3+1)=25
             st.getRangeSum(0, 0) === 3 // 1+2=3
    })()
  )

  console.log(
    "4. 懒加载后单点查询测试:",
    (() => {
      return st.getSingleValue(0) === 3 &&
             st.getSingleValue(2) === 9 &&
             st.getSingleValue(4) === 9
    })()
  )

  console.log(
    "5. 懒加载一致性测试:",
    (() => {
      const sum1 = st.getRangeSum(0, 7)
      const sum2 = st.getRangeSum(0, 3) + st.getRangeSum(4, 7)
      return sum1 === sum2
    })()
  )

  console.log(
    "6. this上下文懒加载测试:",
    (() => {
      const stObj = {
        multiplier: 2,
        processLazyUpdate: function(arr, l, r, val) {
          const st = new SegmentTree(arr)
          st.updateRangeValue(l, r, val)
          const sum = st.getRangeSum(l, r)
          return sum * this.multiplier
        }
      }
      return stObj.processLazyUpdate([1, 2, 3, 4], 0, 3, 1) === 20 // (1+2+3+4+4)*2=20
    })()
  )

  console.log(
    "7. 复杂对象this绑定懒加载测试:",
    (() => {
      const stConfigObj = {
        config: { threshold: 10 },
        processWithConfig: function(arr, l, r, val) {
          const st = new SegmentTree(arr)
          st.updateRangeValue(l, r, val)
          const sum = st.getRangeSum(l, r)
          return sum > this.config.threshold
        }
      }
      return stConfigObj.processWithConfig([1, 2, 3, 4], 0, 3, 2) === true
    })()
  )

  console.log(
    "8. 懒加载性能测试:",
    (() => {
      const largeArr = Array.from({ length: 1000 }, (_, i) => i + 1)
      const perfSt = new SegmentTree(largeArr)
      const start = performance.now()
      for (let i = 0; i < 100; i++) {
        perfSt.updateRangeValue(0, 999, 1)
      }
      const end = performance.now()
      return (end - start) < 1000
    })()
  )

  console.log(
    "9. 懒加载边界测试:",
    (() => {
      const boundarySt = new SegmentTree([1])
      boundarySt.updateRangeValue(0, 0, 5)
      return boundarySt.getRangeSum(0, 0) === 6 &&
             boundarySt.getSingleValue(0) === 6
    })()
  )

  console.log(
    "10. 懒加载复杂测试:",
    (() => {
      const complexSt = new SegmentTree([1, 2, 3, 4, 5, 6, 7, 8])
      complexSt.updateRangeValue(0, 7, 1) // 全部加1
      complexSt.updateRangeValue(0, 3, 2) // 前4个再加2
      complexSt.updateRangeValue(4, 7, 3) // 后4个再加3
      return complexSt.getRangeSum(0, 7) === 60 && // (1+1+2)+(2+1+2)+(3+1+2)+(4+1+2)+(5+1+3)+(6+1+3)+(7+1+3)+(8+1+3)=60
             complexSt.getRangeSum(0, 3) === 24 &&
             complexSt.getRangeSum(4, 7) === 36
    })()
  )
}

testSegmentTreeLazyPropagation()
